/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Icon;

import com.bourre.data.collections.IndexedArray;
import com.bourre.data.iterator.Iterator;

import fever.data.file.File;
import fever.data.libs.BitmapLibLocator;

import fvaswing.components.filechooser.FvFileChooserResources;
import fvaswing.components.FvBitmapIcon;

/**
 * Defines UI informations for a 
 * {@link fvaswing.components.filechooser.FvFileItem} object.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.filechooser.FvFileView
{	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _list : IndexedArray = new IndexedArray();	private static var _init : Boolean = false;
	
	private var _extensions : IndexedArray;
	private var _icon : Icon;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Registers defaults icon views.
	 * 
	 * <p>You must define a "filechooser" repository in your configuration file, 
	 * either icons views are just ignored.
	 * 
	 * <p>Example
	 * {@code
	 *   <repository id="filechooser">
	 * 	   <asset id="as_icon" url="icons/16x16/filesys/icon_as.png" />
	 * 	   <asset id="fla_icon" url="icons/16x16/filesys/icon_fla.png" />
	 * 	   <asset id="html_icon" url="icons/16x16/filesys/icon_html.png" />
	 * 	   <asset id="xml_icon" url="icons/16x16/filesys/icon_xml.png" />
	 * 	   <asset id="jpg_icon" url="icons/16x16/filesys/icon_jpg.png" />
	 * 	   <asset id="php_icon" url="icons/16x16/filesys/icon_php.png" />
	 * 	   <asset id="text_icon" url="icons/16x16/filesys/icon_text.png" />
	 * 	   <asset id="zip_icon" url="icons/16x16/filesys/icon_zip.png" />
	 *   </repository>
	 * }
	 * 
	 * <p>Otherwise you can register your own views with {@link #register} method.
	 */
	public static function registerDefaultViews() : Void
	{
		if( BitmapLibLocator.isRegistredLocator( 'filechooser' ) && !_init )
		{
			var v : FvFileView;
			
			v = FvFileView.register( FvBitmapIcon.create( 'as_icon', 'filechooser' ) );
			v.addExtension( 'as' );
			
			v = FvFileView.register( FvBitmapIcon.create( 'fla_icon', 'filechooser' ) );
			v.addExtension( 'fla' );
			
			v = FvFileView.register( FvBitmapIcon.create( 'html_icon', 'filechooser' ) );
			v.addExtensions( [ 'htm', 'html' ] );
			
			v = FvFileView.register( FvBitmapIcon.create( 'jpg_icon', 'filechooser' ) );
			v.addExtensions( [ 'jpg', 'jpeg', 'gif', 'png', 'bmp' ] );
			
			v = FvFileView.register( FvBitmapIcon.create( 'php_icon', 'filechooser' ) );
			v.addExtensions( [ 'php', 'php3', 'php4', 'asp', 'aspx', 'cfm' ] );
			
			v = FvFileView.register( FvBitmapIcon.create( 'zip_icon', 'filechooser' ) );
			v.addExtensions( [ 'zip', 'rar', 'cab' ] );
			
			v = FvFileView.register( FvBitmapIcon.create( 'swf_icon', 'filechooser' ) );
			v.addExtension( 'swf' );
			
			v = FvFileView.register( FvBitmapIcon.create( 'text_icon', 'filechooser' ) );
			v.addExtensions( [ 'txt', 'doc' ] );
			
			v = FvFileView.register( FvBitmapIcon.create( 'xml_icon', 'filechooser' ) );
			v.addExtension( 'xml' );
			
			_init = true;
		}
	}
	
	/**
	 * Adds new {@link fvaswing.components.filechooser.FvFileItem} 
	 * structure.
	 */
	public static function register( icon : Icon ) : FvFileView
	{
		var v :FvFileView = new FvFileView( icon );
		_list.push( v );
		return v;
	}
	
	/**
	 * Removes passed-in {@link fvaswing.components.filechooser.FvFileItem} 
	 * from list.
	 */
	public static function unregister( view : FvFileView ) : Boolean
	{
		return _list.remove( view );	
	}
	
	/**
	 * Returns registred <strong>AsWng Icon</strong> with passed in 
	 * {@link fvaswing.components.filechooser.FvFileItem} item.
	 * 
	 * <p>If no icon is found, use the default one depending of item type ( file 
	 * or directory )
	 */
	public static function getIcon( file : File ) : Icon
	{
		if( file.isDirectory() ) return FvFileChooserResources.getInstance().folderIcon;
		
		var concreteView : FvFileView;
		
		var it : Iterator = _list.getIterator();
		while( it.hasNext() )
		{
			var view : FvFileView = it.next();
			if( view._isAvailable( file ) ) 
			{
				concreteView = view;
				break;	
			}
		}
		
		if( concreteView && concreteView._icon ) return concreteView._icon;
		else return FvFileChooserResources.getInstance().fileIcon;
	}
	
	/**
	 * Adds a new possible extension to the filter.
	 */
	public function addExtension( extension : String ) : Boolean
	{
		if( !extension )
		{
			throw new Error( 'Extension can\'t be null or undefined' );
		}
		
		return _extensions.push(extension);
	}
	
	/**
	 * Adds extensions list to the filter
	 * 
	 * @param extensions Array list of extensions
	 */
	public function addExtensions( extensions : Array ) : Void
	{
		if( !extensions )
		{
			throw new Error( 'Extensions can\'t be null or undefined' );
		}
		
		var len : Number = extensions.length;
		for( var i : Number = 0; i < len; i++ )
			_extensions.push( extensions[i] );
	}
	
	/**
	 * Returns extensions list defined for this filter.
	 */
	public function getExtensions() : IndexedArray
	{
		return _extensions;	
	}
	
	/**
	 * Removes passed-in extension from filter.
	 */
	public function removeExtension( extension : String ) : Boolean
	{
		return _extensions.remove(extension);
	}
	
	/**
	 * Resets filter extension list.
	 */
	public function clearExtensions() : Void
	{
		_extensions.init();
	}
	
	/**
	 * Returns a string representation of this view.
	 */
	public function toString() : String
	{
		return 'FvFileView for [ ' + _extensions.getElements() + ' ] file types';	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor
	 */
	private function FvFileView( newIcon : Icon )
	{
		_extensions = new IndexedArray();
		_icon = newIcon;
	}
	
	/**
	 * Indicates if passed-in {@link fvaswing.components.filechooser.FvFileChooserItem} 
	 * item is type of current view.
	 * 
	 * @param file {@link fvaswing.components.filechooser.FvFileItem} instance
	 * @return {@code true} if {@code file} is type of current view, otherwise {@code false}
	 */
	private function _isAvailable( file : File ) : Boolean
	{
		return _extensions.objectExists( file.getExtension() );
	}
}